package com.eims.hongbao.fragment;

import java.text.DecimalFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.contans.view.MainHOngbaoListVo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import bls.juejin.cn.R;

public class HongbaoListAdapter extends BaseAdapter {
	private Context context;

	private List<MainHOngbaoListVo> listVos;

	public HongbaoListAdapter(Context context, List<MainHOngbaoListVo> listVos) {
		super();
		this.context = context;
		this.listVos = listVos;
	}

	@Override
	public int getCount() {
		if (listVos != null) {
			return listVos.size();
		}

		return 0;
	}

	@Override
	public Object getItem(int position) {

		return position;
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		ViewHolder holder;
		// 判断View是否为空
		if (view == null) {
			holder = new ViewHolder();

			view = LayoutInflater.from(context).inflate(R.layout.hongbao_list_adapter_item, null);
//			holder.tv_title = (TextView) view.findViewById(R.id.tv_title);
			holder.tv_name = (TextView) view.findViewById(R.id.tv_name);
			holder.tv_detail = (TextView) view.findViewById(R.id.tv_des);
			holder.tv_statu = (TextView) view.findViewById(R.id.tv_statu);
			holder.tv_tishi = (TextView) view.findViewById(R.id.tv_tishi);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		
		Double num = Double.parseDouble(listVos.get(position).getTotal());
		DecimalFormat df = new DecimalFormat("######0.00");
		String decimalBalanceString = df.format(num) + "元";// format 返回的是字符
//		holder.tv_title.setText(decimalBalanceString);
		
		
		final String name = listVos.get(position).getUname();
		// String name = topicBeans.get(position).name;
		final String isname;
		if (isMobileNO(name)) {
			isname = name.substring(0, 3) + "****" + name.substring(7, name.length());
			
		} else {
			isname = name;
		
		}
	
		holder.tv_name.setText(isname);
		
		if ( listVos.get(position).getSq().equals("false")) {
			holder.tv_tishi.setText("发了个普通红包");	
		}else {
			holder.tv_tishi.setText("发了个拼手气红包");	
		}
		

		holder.tv_detail.setText(listVos.get(position).getTilte());
		if (listVos.get(position).getYl().equals("0")) {
			
//			if (listVos.get(position).getSy().equals("0")) {
//				holder.tv_statu.setText("已领完");
//			}else {
//				holder.tv_statu.setText(listVos.get(position).getSy()+"/"+listVos.get(position).getCount());
//				
//			}
			holder.tv_statu.setText("可领取");
			holder.tv_tishi.setVisibility(View.VISIBLE);
			holder.tv_statu.setTextColor(context.getResources().getColor(R.color.main_red));
			
		}else {
			holder.tv_tishi.setVisibility(View.GONE);
			holder.tv_statu.setText("已领取");
			holder.tv_statu.setTextColor(context.getResources().getColor(R.color.black));
			
		}
		
		return view;
	}

	public boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("1[0-9]{10}");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	static class ViewHolder {
		TextView tv_title;
		TextView tv_name;
		TextView tv_detail;
		TextView tv_statu;
		TextView tv_tishi;

	}

	public String formateRate(String rateStr) {
		if (rateStr.indexOf(".") != -1) {

			int num = 0;
			num = rateStr.indexOf(".");

			String dianAfter = rateStr.substring(0, num + 1);
			String afterData = rateStr.replace(dianAfter, "");
			if (afterData.length() < 2) {
				afterData = afterData + "0";
			} else {
				afterData = afterData;
			}
			return rateStr.substring(0, num) + "." + afterData.substring(0, 2);
		} else {
			if (rateStr == "1") {
				return "100";
			} else {
				return rateStr;
			}
		}
	}

}
